using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;

namespace CKS.EWE.ApplicationPages
{
    public partial class CreateEWEPage : LayoutsPageBase
    {
		private SPWeb spWeb;
		protected TextBox Name;
		protected Label Error;
        protected PlaceHolder phMarkupList;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
			this.spWeb = SPControl.GetContextWeb(Context);
			this.Name.MaxLength = 123;
			if (!this.Page.IsPostBack)
			{
				string strName = base.Request.QueryString.Get("Name");
				if (strName != null)
				{
					this.Name.Text = strName;
				}
			}
        }

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			if (string.IsNullOrEmpty(this.Name.Text))
			{
				this.Name.Focus();
				Page.ClientScript.RegisterStartupScript(this.GetType(), "NameFocus", "document.getElementById('" + this.Name.ClientID + "').focus();", true	);
			}
		}

		protected void SubmitBtn_Click(object sender, EventArgs e)
		{
			this.Page.Validate();
			if (!this.Page.IsValid)
			{
				return;
			}
			if (this.Name.Text.Length == 0)
			{
				this.Error.Text = SPHttpUtility.HtmlEncode(SPResource.GetString("CreateWebPageValidTitle", new object[0]));
				return;
			}
			SPList oList = this.spWeb.Lists[new Guid(Request.QueryString["List"])];
			if (oList != null)
			{
				string strTarget = oList.RootFolder.ServerRelativeUrl + "/" + this.Name.Text + ".aspx";
				if (!this.spWeb.GetFile(strTarget).Exists)
				{
					System.IO.StreamReader oStreamReader = new System.IO.StreamReader("C:\\Program Files\\Common Files\\Microsoft Shared\\web server extensions\\12\\TEMPLATE\\DocumentTemplates\\CKSEWE.aspx");
					SPFile oFile = oList.RootFolder.Files.Add(strTarget, oStreamReader.BaseStream);
					oStreamReader.Close();
					SPListItem oItem = oFile.Item;
					IEnumerator oEnumerator = SPContext.Current.FormContext.FieldControlCollection.GetEnumerator();
					try
					{

						while (oEnumerator.MoveNext())
						{
							if (oEnumerator.Current is BaseFieldControl)
							{
								BaseFieldControl oBaseFieldControl = (BaseFieldControl)oEnumerator.Current;
								oItem[oBaseFieldControl.Field.InternalName] = oBaseFieldControl.Value;
							}
						}
						oItem.UpdateOverwriteVersion();
						SPUtility.Redirect(strTarget, SPRedirectFlags.UseSource | SPRedirectFlags.Static, this.Context);
						return;
					}
					finally
					{
						IDisposable oDisposable = oEnumerator as IDisposable;
						if (oDisposable != null)
						{
							oDisposable.Dispose();
						}
					}
				}
				else
				{
					this.Error.Text = SPHttpUtility.HtmlEncode(SPResource.GetString("CreateWebPageDuplicateTitle", new object[0]));
				}
			}
			else
			{
				this.Error.Text = SPHttpUtility.HtmlEncode(SPResource.GetString("CreateWebPageInvalidList", new object[0]));
			}
		}
    }
}
